﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Types;

namespace BannerLib4Net.Build.Nant.Tasks
{
    [TaskName("SeleniumStop")]
    public class SeleniumStop:Task
    {
        ///<summary>
        ///Server Port
        ///</summary>
        [TaskAttribute("port", Required = false)]
        [Int32ValidatorAttribute(MinValue = 0, MaxValue = 60000)]
        public virtual Int32 Port { get; set; }
 
        protected override void ExecuteTask()
        {
       
            String uri = String.Empty;

            if(Port != Int32.MinValue)
            {
                uri = String.Format("http://localhost:{0}/selenium-server/driver/?cmd=shutDown", Port); 
            }
            else
            {
                uri = "http://localhost:4444/selenium-server/driver/?cmd=shutDown";
            }

            LogInfoMessage(String.Format("Using uri: {0}", uri));

            HttpWebRequest httpWebRequest = HttpWebRequest.Create(uri) as HttpWebRequest;
            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            
            LogInfoMessage(String.Format("Selenium shutdown response status code: {0}", httpWebResponse.StatusCode));
            
            httpWebResponse.Close();
            

        }

        /// <summary>
        /// Logs an infomation message to the build
        /// </summary>
        /// <param name="Message">String representing the message to be logged</param>
        private void LogInfoMessage(String Message)
        {
            if (this.Verbose)
            {
                Log(Level.Info, Message);
            }
        }

    }
}
